package negModel;

import negModelExcep.eParteNoFunciona;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;


public class Rueda extends PartePerformance {

	
	private Llanta llanta;
	private Neumatico neumatico;
	
	public Rueda (Llanta llan, Neumatico neu, String detalle, boolean removible){
		this.llanta = llan;
		this.neumatico = neu;
		this.setMaxVU(this.neumatico.getMaxVU()+ this.llanta.getMaxVU());
		this.arreglar();
		this.setPeso(this.neumatico.getPeso()+ this.llanta.getPeso());
		this.setPerformance(this.neumatico.getPerformance()+ this.llanta.getPerformance());
		this.setAportevmax(neumatico.aportevmax()+llanta.aportevmax());
		this.setDetalle(detalle);
		this.setRemovible(removible);
		
		AlgoPesos acumulador = this.neumatico.getPrecio();
		acumulador.agregarMonto(this.llanta.getPrecio());
		this.setPrecio(acumulador);
	}
	
	public Rueda(Element xmlElement) {
		this.setMaxVU(Integer.parseInt(xmlElement.getAttribute("maxVU")));
		this.setVidaUtil(Integer.parseInt(xmlElement.getAttribute("vidaUtil")));
		this.setPeso(Double.parseDouble(xmlElement.getAttribute("peso")));
		this.setAportevmax(Float.parseFloat(xmlElement.getAttribute("aportevmax")));
		this.setDetalle(xmlElement.getAttribute("detalle"));
		this.setPrecio(new AlgoPesos(Double.parseDouble(xmlElement.getAttribute("precio"))));
		this.setRemovible(Boolean.parseBoolean(xmlElement.getAttribute("removible")));
		this.setPerformance(Integer.parseInt(xmlElement.getAttribute("performance")));
						
		ListaPartes lp= new ListaPartes ();
		Fabrica unaFabrica=new Fabrica();
		Parte unaParte;
				
		NodeList nodos=xmlElement.getElementsByTagName("parteInterna");
		for(int nc=0;nc<nodos.getLength();nc++){
			unaParte= unaFabrica.hacerParte(((Element)nodos.item(nc)).getAttribute("Clase"),(Element)nodos.item(nc));
			lp.add(unaParte);				
			}
		
		this.llanta=(Llanta)lp.devolverParte((new Llanta()).getClass());
		this.neumatico=(Neumatico)lp.devolverParte((new Neumatico()).getClass());
		
		
	}
	
	public Element toXml(Document doc) {
		Element xmlElement = doc.createElement("parte");
		xmlElement.setAttribute("Clase","rueda");		
		
		xmlElement.setAttribute("maxVU", ""+ this.getMaxVU());
		xmlElement.setAttribute("vidaUtil", ""+this.getVidaUtil());
		xmlElement.setAttribute("peso", ""+this.getPeso());
		xmlElement.setAttribute("aportevmax", ""+ this.aportevmax());
		xmlElement.setAttribute("detalle", this.getDetalle());
		xmlElement.setAttribute("precio", ""+(this.getPrecio()).getMonto());
		xmlElement.setAttribute("removible", ""+this.esRemovible());
		xmlElement.setAttribute("performance", ""+this.getPerformance());
		
		xmlElement.appendChild(this.llanta.toXml(doc));
		xmlElement.appendChild(this.neumatico.toXml(doc));
		
		
		return xmlElement;
	}
	
	public void arreglar(){
		super.arreglar();
		this.llanta.arreglar();
		this.neumatico.arreglar();
	}
	
	public void gastar () throws eParteNoFunciona{
		this.setVidaUtil(-2);
	}

	public double funcionar() throws eParteNoFunciona {
		this.gastar();
		return (llanta.funcionar()/ neumatico.funcionar());
	}

	public double getCoeficiente(){
		return neumatico.getCoeficiente();
	}
	
	public double getSuperficie (){
		return neumatico.getPerimetro()* neumatico.getAncho();
	}
	
	public Neumatico getNuematico(){
		return this.neumatico;
	}
	
	public Llanta getLlanta(){
		return this.llanta;
	}
	
	public void agregarMejora(Vehiculo v) {
		
		super.agregarMejora(v);
		((Auto)v).setRueda(this);
		
	}
}
